package com.practice.springboot.rabbitmqtest.config;

import org.springframework.amqp.core.AmqpAdmin;
import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.DirectExchange;
import org.springframework.amqp.core.Exchange;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.core.TopicExchange;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;

/**
 * @Description : Topic模式交换机 RabbitMQ配置
 * @Version : V1.0.0
 * @Date : 2021/11/9 17:14
 */
@Configuration
public class TopicModelRabbitConfig {

    @Value("${spring.rabbitmq.host}")
    private String host;

    @Value("${spring.rabbitmq.port}")
    private int port;

    @Value("${spring.rabbitmq.username}")
    private String username;

    @Value("${spring.rabbitmq.password}")
    private String password;

    @Value("${spring.rabbitmq.virtual-host}")
    private String vHost;

    @Resource
    private AmqpAdmin amqpAdmin;

    // 交换机
//    public static final String TestTopicExchange = "TestTopicExchange";
    public static final String TestTopicExchange = "am-device-alarm-event-exchange";

//    public static final String TestTopicQueue1 = "TestTopicQueue1";
    public static final String TestTopicQueue1 = "am-device-alarm-event-to-am-person-queue";

    public static final String TestTopicRouteKey = "am-device-alarm-event-routingKey";

//    public static final String TestTopicQueue2 = "TestTopicQueue2";

//    @Bean
//    public TopicExchange topicExchange() {
//        return new TopicExchange(TestTopicExchange, true, false);
//    }

//    @Bean
//    public Queue firstQueue() {
//        return new Queue(TestTopicQueue1, true);
//    }
//
//    @Bean
//    public Queue secondQueue() {
//        return new Queue(TestTopicQueue2, true);
//    }

    /**
     * 将firstQueue和topicExchange绑定,而且绑定的键值为topic.man
     * 这样只要是消息携带的路由键是topic.man,才会分发到该队列
     */
//    @Bean
//    public Binding bindingFirst() {
//        return BindingBuilder.bind(firstQueue()).to(topicExchange()).with("am-device-alarm-event-routingKey");
//    }

    /**
     * 将secondQueue和topicExchange绑定,而且绑定的键值为用上通配路由键规则topic.#
     * 这样只要是消息携带的路由键是以topic.开头,都会分发到该队列;
     * binding key 中可以存在两种特殊字符 *与 #，用于做模糊匹配，其中 * 用于匹配一个单词，# 用于匹配0个或多个单词，单词以符号“.”为分隔符。
     */
//    @Bean
//    public Binding bindingSecond() {
//        return BindingBuilder.bind(secondQueue()).to(topicExchange()).with("topic.#");
//    }

    @PostConstruct
    public void init() {
        Exchange amAlarmExchange = new TopicExchange(TestTopicExchange, true, false);
        amqpAdmin.declareExchange(amAlarmExchange);

        amqpAdmin.declareQueue(new Queue(TestTopicQueue1, true));
        amqpAdmin.declareBinding(new Binding(TestTopicQueue1, Binding.DestinationType.QUEUE,
                TestTopicExchange, "am-device-alarm-event-routingKey", null));
    }

}
